package com.taotao.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.taotao.common.pojo.EasyUIDataGridResult;
import com.taotao.common.pojo.TaotaoResult;
import com.taotao.common.utils.IDUtils;
import com.taotao.mapper.TbItemDescMapper;
import com.taotao.mapper.TbItemMapper;
import com.taotao.mapper.TbItemParamItemMapper;
import com.taotao.pojo.TbItem;
import com.taotao.pojo.TbItemDesc;
import com.taotao.pojo.TbItemExample;
import com.taotao.pojo.TbItemExample.Criteria;
import com.taotao.pojo.TbItemParamItem;
import com.taotao.pojo.TbItemParamItemExample;
import com.taotao.service.ItemService;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ItemServiceImpl implements ItemService {
	@Autowired
	private TbItemMapper tbItemMapper;
	@Autowired
	TbItemDescMapper tbItemDescMapper;
	@Autowired
	TbItemParamItemMapper tbItemParamItemMapper;

	public TbItem getItemById(Long itemId) {
		TbItemExample example = new TbItemExample();
		Criteria criteria = example.createCriteria();
		criteria.andIdEqualTo(itemId);
		List<TbItem> tbItems = tbItemMapper.selectByExample(example);
		TbItem tbItem = null;
		if ((tbItems.size() > 0) && (tbItems != null)) {
			tbItem = (TbItem) tbItems.get(0);
		}
		return tbItem;
	}

	public EasyUIDataGridResult getItemList(int page, int rows) {
		PageHelper.startPage(page, rows);

		TbItemExample example = new TbItemExample();
		List<TbItem> list = tbItemMapper.selectByExample(example);

		PageInfo<TbItem> info = new PageInfo<>(list);

		EasyUIDataGridResult result = new EasyUIDataGridResult();
		result.setTotal(info.getTotal());
		result.setRows(list);

		return result;
	}

	public TaotaoResult createItem(TbItem item, String desc, String itemParams) {
		long id = IDUtils.genItemId();
		item.setId(id);

		item.setStatus((byte) 1);

		Date date = new Date();
		item.setCreated(date);
		item.setUpdated(date);

		tbItemMapper.insert(item);

		TbItemDesc tbItemDesc = new TbItemDesc();
		tbItemDesc.setItemId(id);
		tbItemDesc.setItemDesc(desc);
		tbItemDesc.setCreated(date);
		tbItemDesc.setUpdated(date);

		tbItemDescMapper.insert(tbItemDesc);

		TbItemParamItem tbItemParamItem = new TbItemParamItem();
		tbItemParamItem.setItemId(id);
		tbItemParamItem.setParamData(itemParams);
		tbItemParamItem.setCreated(date);
		tbItemParamItem.setUpdated(date);

		tbItemParamItemMapper.insert(tbItemParamItem);

		return TaotaoResult.ok();
	}

	public TaotaoResult itemUpdate(TbItem item, String desc, String itemParams) {
		Date update = new Date();
		long itemId = item.getId();

		item.setUpdated(update);
		tbItemMapper.updateByPrimaryKeySelective(item);

		TbItemDesc record = new TbItemDesc();
		record.setItemId(itemId);
		record.setUpdated(update);
		record.setItemDesc(desc);
		tbItemDescMapper.updateByPrimaryKeySelective(record);

		TbItemParamItemExample example = new TbItemParamItemExample();
		com.taotao.pojo.TbItemParamItemExample.Criteria criteria = example
				.createCriteria();

		criteria.andItemIdEqualTo(itemId);

		List<TbItemParamItem> list = this.tbItemParamItemMapper
				.selectByExampleWithBLOBs(example);
		if (list.size() == 0) {
			TbItemParamItem tbItemParamItem = new TbItemParamItem();
			tbItemParamItem.setItemId(itemId);
			tbItemParamItem.setParamData(itemParams);
			tbItemParamItem.setCreated(update);
			tbItemParamItem.setUpdated(update);

			tbItemParamItemMapper.insert(tbItemParamItem);
			return TaotaoResult.ok();
		}
		TbItemParamItem record1 = new TbItemParamItem();
		record1.setParamData(itemParams);
		record1.setUpdated(update);
		tbItemParamItemMapper.updateByExampleSelective(record1, example);

		return TaotaoResult.ok();
	}

	public TaotaoResult deleteItem(long[] ids) {
		for (long l : ids) {
			tbItemMapper.deleteByPrimaryKey(l);
		}
		return TaotaoResult.ok();
	}

	public TaotaoResult itemsInstock(long[] ids) {
		for (long l : ids) {
			TbItem record = new TbItem();
			record.setId(l);
			record.setStatus((byte) 2);
			tbItemMapper.updateByPrimaryKeySelective(record);
		}
		return TaotaoResult.ok();
	}

	public TaotaoResult itemsReshelf(long[] ids) {
		for (long l : ids) {
			TbItem record = new TbItem();
			record.setId(l);
			record.setStatus((byte) 1);
			tbItemMapper.updateByPrimaryKeySelective(record);
		}
		return TaotaoResult.ok();
	}
}
